const fs = require('fs');
const path = require('path');
const SudokuGenerator = require('./sudoku-generator');

const generator = new SudokuGenerator();

function puzzleToString(board) {
    // 将二维数组转为81位字符串
    return board.flat().join('');
}

function genInsertSQL(puzzles, level) {
    return puzzles.map(p => {
        const puzzleStr = puzzleToString(p.puzzle);
        const solutionStr = puzzleToString(p.solution);
        return `INSERT INTO sudoku_puzzles (level, puzzle, solution, clues, created_at, is_deleted) VALUES ('${level}', '${puzzleStr}', '${solutionStr}', ${p.clues}, NOW(), '0');`;
    }).join('\n');
}

const levels = [
    { name: 'easy', count: 100 },
    { name: 'medium', count: 100 },
    { name: 'hard', count: 100 }
];

let allSQL = '';

levels.forEach(({ name, count }) => {
    console.log(`正在生成 ${count} 条 ${name} 难度题目...`);
    const puzzles = generator.generate(name, count);
    allSQL += `-- ${name} 难度题目\n`;
    allSQL += genInsertSQL(puzzles, name) + '\n\n';
});

const outputPath = path.resolve(__dirname, '../../../db/data.sql');
fs.writeFileSync(outputPath, allSQL, 'utf8');
console.log(`SQL 已生成到 ${outputPath}`);